package com.digitaldiary.bean;

import com.digitaldiary.entity.DiaryUser;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceUnit;
import javax.persistence.Query;
import javax.transaction.*;


@ManagedBean(name = "userHandler")
@SessionScoped
public class UserHandler {
    
    @PersistenceUnit
    private EntityManagerFactory entityManagerFactory;
    @Resource
    private UserTransaction userTransaction;

    public DiaryUser user;
    
    public String id;
    public String firstName;
    public String lastName;
    public String email;
    public String gender;
    public String dob;
    public String userName;
    public String password1;
    public String password2;
    public String phoneNumber;
    
    @PostConstruct
    public void init() {
        System.out.println("thbis  asdasd");
    }
    
    public void getUser(String id){
        EntityManager entityManager = entityManagerFactory.createEntityManager();
        try{
            user = new DiaryUser();
            user = entityManager.find(DiaryUser.class, id);
        } catch (Throwable e) {
            e.printStackTrace();
        }
        
    }
    
    // all methods
    public void saveUser() {
        EntityManager entityManager = entityManagerFactory.createEntityManager();
        
        try {
            userTransaction.begin();
            user = new DiaryUser();
            user.setFirstName(firstName);
            user.setLastName(lastName);
            user.setEmail(email);
            user.setGender(gender);
            user.setDob(dob);
            user.setUserName(userName);
            user.setPassword1(password1);
            user.setPassword2(password2);
            user.setPhoneNumber(phoneNumber);
            entityManager.persist(user);
            userTransaction.commit();
            String url = "faces/signupSuccess.xhtml";
            FacesContext fc = FacesContext.getCurrentInstance();
            ExternalContext ec = fc.getExternalContext();
            try {
                ec.redirect(url);
            } catch (Throwable ex) {
                
            }
        } catch (NotSupportedException e) {
            e.printStackTrace();
        } catch (SystemException e) {
            e.printStackTrace();
        } catch (SecurityException e) {
            e.printStackTrace();
        } catch (IllegalStateException e) {
            e.printStackTrace();
        } catch (RollbackException e) {
            e.printStackTrace();
        } catch (HeuristicMixedException e) {
            e.printStackTrace();
        } catch (HeuristicRollbackException e) {
            e.printStackTrace();
        }
    }
    
   public DiaryUser checkUser(String username)
   {
       DiaryUser checkedUser=null;
       System.out.println("Hello");
       try{
           EntityManager entityManager= entityManagerFactory.createEntityManager();
           Query query= entityManager.createQuery("Select s from DiaryUser s where s.userName=:userName");
           query.setParameter("userName",username);
           checkedUser= (DiaryUser) query.getSingleResult();
           return checkedUser;
       }catch(NoResultException e){
        e.printStackTrace();
       }
       return checkedUser;
   }

    public DiaryUser getUser() {
        return user;
    }

    public void setUser(DiaryUser user) {
        this.user = user;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public String getDob() {
        return dob;
    }

    public void setDob(String dob) {
        this.dob = dob;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getPassword1() {
        return password1;
    }

    public void setPassword1(String password1) {
        this.password1 = password1;
    }

    public String getPassword2() {
        return password2;
    }

    public void setPassword2(String password2) {
        this.password2 = password2;
    }

    public String getPhoneNumber() {
        return phoneNumber;
    }

    public void setPhoneNumber(String phoneNumber) {
        this.phoneNumber = phoneNumber;
    }
   
    
    
    
    
}
